package com.classtest02;

import java.math.BigDecimal;

public class Test{
    public static void main(String[] args) {
        //定义一个随机小数0-100
        double num = Math.random()*100;
        System.out.println("原数字");
        System.out.println(num);
        System.out.println("取整后");
        System.out.println(proTwo(num));
        //保留四位小数，四舍五入后
        System.out.println("保留四位小数，四舍五入后");
        System.out.println(sishewuru(num));

//        System.out.println("取整后");
//        System.out.println(new HandleableImpl().handleString(bd.toString()));
//        System.out.println("原数字");
//        System.out.println(num);
//        //保留四位小数，四舍五入后
//        System.out.println("保留四位小数，四舍五入后"+new HandleableImpl().handle(bd.toString()));
    }

    public static String proTwo(double num) {
        BigDecimal bd = new BigDecimal(num);
        //取整后
        //匿名内部类调用Handleable接口取整
        //使用lambda表达式改写
        Handleable hnum = ((Handleable) -> {
            BigDecimal bd1 = new BigDecimal(num);
            return bd1.setScale(0,1).toString();
        });
        return hnum.handleString(bd.toString());
    }
    public static String sishewuru(double num){
        BigDecimal bd = new BigDecimal(num);
        Handleable hnum2 = ((Handleable) -> {
            BigDecimal bd1 = new BigDecimal(num);
            return bd1.setScale(4,1).toString();
        });
        return hnum2.handleString(bd.toString());
    }
}

//class HandleableImpl implements Handleable{
//    @Override
//    public String handleString(String num) {
//        BigDecimal bd = new BigDecimal(num);
//        return bd.setScale(0,1).toString();
//    }
//    public String handle(String num){
//        BigDecimal bd = new BigDecimal(num);
//        return bd.setScale(4,4).toString();
//    }
//}
